package com.huarui.saleOrder.action;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;

import com.huarui.purchaseOrder.model.PurOrSaleOrder;
import com.huarui.purchaseOrder.model.PurOrSaleOrderDetail;
import com.huarui.saleOrder.service.SaleOrderService;
import com.huarui.util.FuncUtil;

public class SaleOrderUpdateAction extends BaseAction{

	public static final String MESS = "mess";
	
	private static final long serialVersionUID = -4381254943694916832L;

	private Logger logger = Logger.getLogger(SaleOrderUpdateAction.class);
	
	private SaleOrderService saleOrderService;

	private String saleOrder;
	
	private String add;
	
	private String all;
	
	private String update;
	
	private Map<String, Object> mesMap ;
	
	
	public String getSaleOrder() {
		return saleOrder;
	}


	public void setSaleOrder(String saleOrder) {
		this.saleOrder = saleOrder;
	}


	public String getAdd() {
		return add;
	}


	public void setAdd(String add) {
		this.add = add;
	}


	public String getAll() {
		return all;
	}


	public void setAll(String all) {
		this.all = all;
	}


	public String getUpdate() {
		return update;
	}


	public void setUpdate(String update) {
		this.update = update;
	}


	public Map<String, Object> getMesMap() {
		return mesMap;
	}


	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}


	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}


	/**
	 * 修改销售订单
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private String updateSaleOrderAndDetails() {
		JSONObject jo_sale = JSONObject.fromObject(saleOrder);
		FuncUtil.removeNullValue(jo_sale);
		PurOrSaleOrder obj_sale = (PurOrSaleOrder) JSONObject.toBean(jo_sale, PurOrSaleOrder.class);
		
		JSONArray ja_add = JSONArray.fromObject(add);
		FuncUtil.removeNullValue(ja_add);
		List<PurOrSaleOrderDetail> list_add = (List<PurOrSaleOrderDetail>) JSONArray.toCollection(ja_add, PurOrSaleOrderDetail.class);
		
		JSONArray ja_all = JSONArray.fromObject(all);
//		FuncUtil.removeNullValue(ja_all);
		List<Integer> list_all = (List<Integer>) JSONArray.toCollection(ja_all);
		
		JSONArray ja_update = JSONArray.fromObject(update);
		FuncUtil.removeNullValue(ja_update);
		List<PurOrSaleOrderDetail> list_update = (List<PurOrSaleOrderDetail>) JSONArray.toCollection(ja_update, PurOrSaleOrderDetail.class);
		
		mesMap = this.saleOrderService.updateSaleOrderAndDetails(obj_sale, list_update, list_add, list_all);
		
		return MESS;
	}
	
	
	public String updateOrdersAndDetails() {
		return updateSaleOrderAndDetails();
	}
	
	public String updateToSubmitOrdersAndDetails() {
		return updateSaleOrderAndDetails();
	}
}
